<?php


namespace app\jiangnan\controller;


use app\utils\JwtUtils;
use think\Controller;
use think\Db;

class Login extends Controller
{
    public function bindRoom()
    {
        $accessToken=request()->header("accessToken");
        if(empty($accessToken)){
            return json(["code"=>10020,'msg' => '用户未登录']);
        }

        $userResult=JwtUtils::getDecodeToken($accessToken);
        $userInfo=Db::table("userinfo")->where("userid",$userResult["userid"])->find();
        if(!empty($userInfo)){
            if(!empty($userInfo["room_id"])){
                return json(["code"=>1,'msg' => '用户已经绑定']);
            }
        }
        $str=request()->param("str");
        $result=openssl_decrypt(base64_decode($str), 'AES-128-CBC', base64_decode("R4rN2UTo28db7lwO1zNGhA=="),OPENSSL_RAW_DATA);
        $result= json_decode($result, true);
        if($result["code"]!="10000"){
            return json(["code"=>1,'msg' => '用户手机号获取失败']);
        }
        $mobile=$result["mobile"];
        $data=request()->post();
        //执行绑定房屋操作
        $roominfo=Db::table("roominfo")
            ->where("phone",$mobile)
            ->find();
        if(empty($roominfo))
        {
            return json(["code"=>0,'msg' => '房屋绑定失败，请重新绑定1']);
        }
        if(empty($userInfo)){
            $add_info["userid"]=$userResult["userid"];
            $add_info["room_id"]=$roominfo["id"];
            $add_info["insert_time"]=date('Y-m-d H:i:s');
            $add_info["update_time"]=date('Y-m-d H:i:s');
            //Log::write($data,"yang");
            $res=Db::table("userinfo")->insertGetId($add_info);
            if($res)
            {

                $add_info["bindRoomStatus"]=true;

                $token=JwtUtils::getToken($add_info);
                return json(["code"=>0,'msg' => '房屋绑定成功',"accessToken"=>$token]);
            }else
            {
                return json(["code"=>1,'error' => '房屋绑定失败，请重新获取验证码绑定3']);
            }
        }else{
            $res=Db::table("userinfo")
                ->where("id",$userInfo["id"])
                ->update(["room_id"=>$roominfo["id"]]);
            if($res)
            {
                $userInfo["room_id"]=$roominfo["id"];
                $userInfo["bindRoomStatus"]=true;

                $token=JwtUtils::getToken($userInfo);
                return json(["code"=>0,'msg' => '房屋绑定成功',"accessToken"=>$token]);
            }else
            {
                return json(["code"=>1,'error' => '房屋绑定失败']);
            }
        }


    }
    //业主绑定房屋
    public function bindRoom2()
    {
        $accessToken=request()->header("accessToken");
        if(empty($accessToken)){
            return json(["code"=>10020,'msg' => '用户未登录']);
        }

        $userResult=JwtUtils::getDecodeToken($accessToken);
        $userInfo=Db::table("userinfo")->where("userid",$userResult["userid"])->find();
        if(!empty($userInfo)){
            if(!empty($userInfo["room_id"])){
                return json(["code"=>1,'msg' => '用户已经绑定']);
            }
        }
        $data=request()->post();
        //手机号验证
        $smsverify=Db::table("sms_verify")
            ->where("phone",$data["phone"])
            ->where("code",$data["verificationCode"])
            ->where("ctime",'>',(time()-80))
            ->order("id desc")
            ->find();
        if(empty($smsverify))
        {
            return json(["code"=>1,'msg' => '验证码错误']);
        }else
        {
            $info["status"]="2";
            Db::table("sms_verify")
                ->where("id",$smsverify["id"])
                ->update($info);
            //执行绑定房屋操作
            $roominfo=Db::table("roominfo")
                ->where("phone",$data["phone"])
                ->find();
            if(empty($roominfo))
            {
                return json(["code"=>0,'msg' => '房屋绑定失败，请重新获取验证码绑定1']);
            }
            if(empty($userInfo)){
                $add_info["userid"]=$userResult["userid"];
                $add_info["room_id"]=$roominfo["id"];
                $add_info["insert_time"]=date('Y-m-d H:i:s');
                $add_info["update_time"]=date('Y-m-d H:i:s');
                //Log::write($data,"yang");
                $res=Db::table("userinfo")->insertGetId($add_info);
                if($res)
                {

                    $add_info["bindRoomStatus"]=true;

                    $token=JwtUtils::getToken($add_info);
                    return json(["code"=>0,'msg' => '房屋绑定成功',"accessToken"=>$token]);
                }else
                {
                    return json(["code"=>1,'error' => '房屋绑定失败，请重新获取验证码绑定3']);
                }
            }else{
                $res=Db::table("userinfo")
                    ->where("id",$userInfo["id"])
                    ->update(["room_id"=>$roominfo["id"]]);
                if($res)
                {
                    $userInfo["room_id"]=$roominfo["id"];
                    $userInfo["bindRoomStatus"]=true;

                    $token=JwtUtils::getToken($userInfo);
                    return json(["code"=>0,'msg' => '房屋绑定成功',"accessToken"=>$token]);
                }else
                {
                    return json(["code"=>1,'error' => '房屋绑定失败，请重新获取验证码绑定4']);
                }
            }

        }
    }


    //业主获取短信验证码
    public function getSmsCode()
    {
        //return json(["code"=>0,'msg' => '验证码发送成功']);
        $data=request()->post();
        //查询该用户是否绑定房屋

        //查询该手机号下是否存在房屋
        $roominfo=Db::table("roominfo")->where("phone",$data["phone"])->find();
        if(empty($roominfo))
        {
            return json(["code"=>500,'msg' => '亲爱的业主：您的手机号码验证错误，可能是预留在物业服务中心手机号码没有及时更新，为更快的解决您的问题，您可以在线客服留言或致电我们，感谢您的理解支持。']);
        }
        //查询该房屋是否已经绑定用户
        $userinfo=Db::table("userinfo")
            ->where("room_id",$roominfo["id"])
            ->find();
        if(!empty($userinfo))
        {
            return json(["code"=>500,'msg' => '该房屋已经绑定']);
        }

        $result=$this->sendmsg($data["phone"]);
        return $result;
    }

    //短信验证码
    public function sendmsg($phone)
    {
        $code=rand(100000,999999);
        //$phone=request()->param("phone");
        $send = send_sms($phone, 1, $code);
        if($send['Message'] == 'OK')
        {
            $info["phone"]=$phone;
            $info["code"]=$code;
            $info["ctime"]=time();
            $result=Db::table("sms_verify")->insert($info);
            if($result)
            {
                return json(["code"=>0,'msg' => '验证码发送成功']);

                //die;
            }else
            {
                return json(["code"=>500,'msg' => '请重新获取验证码']);
            }
        } else
        {
            return json(["code"=>500,'msg' => $send['Message']]);

        }
    }
}